package com.bbm.communitysystem.controller.waste;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Waste;
import com.bbm.communitysystem.domain.dto.WasteDto;
import com.bbm.communitysystem.service.IWastePriceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-28
 */
@Tag(name = "废品控制器")
@RestController
@RequestMapping("/waste")
@RequiredArgsConstructor
public class WasteController {

    private final IWastePriceService wastePriceService;

    @Operation(summary = "新增废品")
    @PostMapping
    public R<String> addWastePrice(@RequestBody WasteDto wasteDto) {

        Waste waste = BeanUtil.copyProperties(wasteDto, Waste.class);

        waste.setCreateBy(waste.getUpdateBy());
        waste.setCreateTime(System.currentTimeMillis() + "");
        waste.setUpdateTime(waste.getCreateTime());
        waste.setStatus(1);

        boolean save = wastePriceService.save(waste);

        return save ? R.ok("新增成功") : R.error("新增失败");
    }

    @Operation(summary = "分页查询废品")
    @GetMapping
    @Parameter(name = "page", description = "当前页")
    @Parameter(name = "size", description = "每页大小")
    public R<Page<Waste>> getWasteByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return R.ok(wastePriceService.page(new Page<>(page, size)));
    }

    @Operation(summary = "更新废品信息")
    @PutMapping
    public R<String> updateWaste(@RequestBody WasteDto dto) {

        Waste byId = wastePriceService.getById(dto.getId());

        if (byId == null) {
            return R.error("该废品不存在不存在");
        }

        boolean success = wastePriceService.lambdaUpdate()
                .set(dto.getWasteClassId() != null, Waste::getWasteClassId, dto.getWasteClassId())
                .set(dto.getWasteName() != null && !dto.getWasteName().isEmpty(), Waste::getWasteName, dto.getWasteName())
                .set(dto.getWastePrice() != null && dto.getWastePrice() != 0, Waste::getWastePrice, dto.getWastePrice())
                .set(dto.getStatus() != null, Waste::getStatus, dto.getStatus())
                .set(Waste::getLastWastePrice, byId.getWastePrice())
                .set(Waste::getUpdateTime, System.currentTimeMillis() + "")
                .eq(Waste::getId, dto.getId())
                .update();

        return success ? R.ok("更新成功") : R.error("更新失败");
    }

    @Operation(summary = "删除废品")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "废品id")
    public R<String> deleteWaste(@PathVariable Integer id) {
        return wastePriceService.removeById(id) ?
                R.ok("删除成功") : R.error("删除失败");
    }


    @Operation(summary = "获取今日该品类4条最高价格")
    @GetMapping("/price/{wasteClassId}")
    public R getWasteByPrice(@PathVariable("wasteClassId") Integer wasteClassId) {

        List<Waste> list = wastePriceService.lambdaQuery()
                .eq(Waste::getWasteClassId, wasteClassId)
                .eq(Waste::getStatus, 1)
                .orderByDesc(Waste::getWastePrice)
                .last("limit 4")
                .list();

        return R.ok(list);
    }

}
